<?php

/**
 * Form Element class
 * 
 * @author Mathieu Pheulpin - sokaru91@gmail.com
 *
 */
abstract class Mac_Form_Element_Abstract
{
    const REQUIRED_MESSAGE = 'Ce champ est obligatoire';

    protected $id = null;

    protected $name = null;
    
    protected $value = null;
    
    protected $required = false;
    
    protected $label = null;
    
    protected $class = array();

    protected $options = array();
    
    /**
     * Validators
     * @var array<Form_Validator_Abstract>
     */
    protected $validators = array();
    
    /**
     * Error and/or Warning messages
     * @var array<string>
     */
    protected $messages = array();
    
    public function __construct ($name)
    {
        $this->id = $name;
        $this->name = $name;
    }

    public function setId ($id)
    {
        $this->id = $id;
    }

    public function getId ()
    {
        return $this->id;
    }

    public function getName ()
    {
        return $this->name;
    }
    
    public function setValue ($value)
    {
        $this->value = $value;
        return $this;
    }
    
    public function getValue ()
    {
        return $this->value;
    }
    
    public function isRequired ()
    {
        return (bool)$this->required;
    }
    
    public function setRequired ($required = true)
    {
        $this->required = (bool)$required;
        return $this;
    }
    
    public function addValidator (Mac_Form_Validator_Abstract $validator)
    {
        $validator->setElement($this);
        $this->validators[] = $validator;
        return $this;
    }
    
    public function isValid ($data)
    {
        if ($this->value === '') {
            if ($this->isRequired()) {
                $this->messages[] = self::REQUIRED_MESSAGE;
                return false;
            } else {
                return true;
            }
        }
        $valid = true;
        foreach($this->validators as $validator) {
            $thisValid = $validator->isValid($this->value, $data);
            $valid &= $thisValid;
            if (!$thisValid) {
                $this->messages[] = $validator->getMessage();
            }
        }
        return $valid;
    }
    
    public function setLabel ($value)
    {
        $this->label = $value;
        return $this;
    }
    
    public function getLabel ()
    {
        return $this->label;
    }
    
    public function addClass ($class)
    {
        $this->class[] = $class;
        return $this;
    }
    
    public function setClasses (array $classes)
    {
        $this->class = $classes;
        return $this;
    }

    public function getMessages ()
    {
        return $this->messages;
    }

    public function setOptions (array $options)
    {
        $this->options = $options;
    }

    public function addOption ($name, $value)
    {
        $this->options[$name] = $value;
    }

    protected function printOptions ()
    {
        $return = array();
        foreach($this->options as $name => $value) {
            $return[] = $name.'="'.$value.'"';
        }
        return implode(' ', $return);
    }
}
